<?php
namespace app\admin\service;

use app\model\Seller;
use app\model\SellerType;
use app\model\User;
use app\model\Goods;
use app\model\Charge;
use app\model\SystemMessage;
use think\Validate;
use think\Session;

class SellerService{
    public function index($param)
    {
        // dump($param);die;
        if(!array_key_exists("key",$param)){
            $key = '';
        }else{
            $key = $param['key'];
        }
        $sellerModel = new Seller;
        if(!$key){
            $sellers = $sellerModel::with([
                'sellerType' =>function($query){
                    $query->field('id,name');
                }
            ])->where('status',2)->order('id desc')->paginate(10,false,['query'=>$param]);
        }else{
            $sellers = $sellerModel::with([
                'sellerType' =>function($query){
                    $query->field('id,name');
                }
            ])->where('status',2)->where('mobile|username|name','like','%'.$key.'%')->order('id desc')->paginate(10,false,['query'=>$param]);
        }
        $page = $sellers->render();
        return ['sellers'=>$sellers,'page'=>$page,'key'=>$key];
    }

    public function sellerGoodsList($param)
    {
        $seller_id = $param['seller_id'];
        $goodsModel = new Goods;
        $lists = $goodsModel::where('seller_id',$seller_id)->order('add_time desc')->paginate(10,false,['query'=>$param]);
        $page = $lists->render();
        return ['lists'=>$lists,'page'=>$page];
    }

    public function applys($param)
    {
        $sellerModel = new Seller;
        $applys = $sellerModel::where('status',1)->order('id desc')->paginate(10,false,['query'=>$param]);
        $page = $applys->render();
        $sellerTypes = $this->sellerType();
        // dump($sellerTypes);die;
        return ['applys'=>$applys,'page'=>$page,'sellerTypes'=>$sellerTypes];
    }

    public function examineApply($post)
    {
        // dump($post);die;
        $sellerModel = new Seller;
        $id = $post['seller_id'];
        $seller = $sellerModel::get($id);
        unset($post['seller_id']);
        $post['examine_time'] = time();
        $info_seller = $sellerModel::where('id',$id)->update($post);
        if(!$info_seller){
            return ['code'=>0,'msg'=>'系统繁忙'];
        }
        $userModel = new User;
        $info_user = $userModel::where('id',$seller->uid)->update(['user_type_id'=>3]);
        if(!$info_user){
            $sellerModel::where('id',$id)->update(['seller_type_id'=>0,'status'=>1,'examine_time'=>null]);
            return ['code'=>-1,'msg'=>'系统繁忙'];
        }
        return ['code'=>1,'msg'=>'已审核'];
    }

    public function sellerType()
    {
        $sellerTypeModel = new SellerType;
        $sellerTypes = $sellerTypeModel::all();
        return $sellerTypes;
    }

    public function addSellerTypePost($post,$type = 1)//$type 1:代表新增，2：代表编辑
    {
        $rule = [
            'name'  => 'require',
        ];

        $msg = [
            'name.require' => '商户分类名称必须',
        ];
        $validate = new Validate($rule, $msg);
        if(!$validate->check($post)){
            return ['code'=>0,'msg'=>$validate->getError()];
        }
        $post['add_time'] = time();
        $sellerTypeModel = new SellerType;
        if($type == 1){
            $info = $sellerTypeModel::insert($post);
            if(!$info){
                return ['code'=>-1,'msg'=>'系统繁忙'];
            }
            return ['code'=>1,'msg'=>'新增成功'];
        }else{
            $id = $post['id'];
            unset($post['id']);
            $info = $sellerTypeModel::where('id',$id)->update($post);
            if(!$info){
                return ['code'=>-1,'msg'=>'系统繁忙'];
            }
            return ['code'=>1,'msg'=>'编辑成功'];
        }
    }

    public function editSellerType($id)
    {
        $sellerTypeModel = new SellerType;
        $sellerType = $sellerTypeModel::get($id);
        return $sellerType;
    }

    public function delSellerType($id)
    {
        // dump($id);die;
        $sellerTypeModel = new SellerType;
        $info = $sellerTypeModel::where('id',$id)->delete();
        if(!$info){
            return ['code'=>0,'msg'=>'系统繁忙'];
        }
        return ['code'=>1,'msg'=>'删除成功'];
    }

    public function charge($param)
    {
        $str = '';
        $status = 0;
        if(array_key_exists('status',$param) && $param['status'] != 0){
            $str = 'status = '.$param['status'];
            $status = $param['status'];
        }
        // dump($str);die;
        $chargeModel = new Charge;
        $charges = $chargeModel::with([
            'seller'   => function($query){
                $query->field('id,mobile,name,username');
            }
        ])->where($str)->order('add_time desc')->paginate(10,false,['query'=>$param]);
        // dump($charges);die;
        $page = $charges->render();
        return ['charges'=>$charges,'page'=>$page,'status'=>$status];
    }

    public function picture($id)
    {
        $chargeModel = new Charge;
        $charge = $chargeModel::find($id);
        return [
            'title'  => '打款截图',
            'id'     => $id,
            'status' => 0,
            'data'   => [
                [
                  'alt'   => '打款截图',
                  'pid'   => $id,
                  'src'   => $charge->picture,
                  'thumb' => $charge->picture,
                ]
            ],
        ];
    }

    public function chargeExamine($post)
    {
        $chargeModel = new Charge;
        $id = $post['id'];
        if($post['status'] == 3){//审核拒绝
            if(!$post['content']){
                return ['code'=>-2,'msg'=>'请填写拒绝的原因'];
            }
            $charge = $chargeModel::get($id);
            $systemMessageModel = new SystemMessage;
            $info_system_message = $systemMessageModel::insertGetId([
                'uid'       => $charge->uid,
                'type'      => 2,
                'admin_id'  => Session::get('uid'),
                'relate_id' => $id,
                'content'   => $post['content'],
                'add_time'  => time(),
            ]);
            if(!$info_system_message){
                return ['code'=>-3,'msg'=>'系统繁忙'];
            }
        }
        $data['status'] = $post['status'];
        $data['examine_time'] = time();
        $info = $chargeModel::where('id',$id)->update($data);
        if(!$info){
            if($post['status'] == 3){
                $systemMessageModel::where('id',$info_system_message)->delete();
            }
            return ['code'=>-1,'msg'=>'系统繁忙'];
        }
        return ['code'=>1,'msg'=>'已审核'];
    }
}
